from math import cos
import random
import pygame
from pygame.locals import *
from sys import exit
from gameobjects.vector2 import Vector2

### VARIABLES ###
screenSize = 300
numberEnemies = 10
speed = 100
enemySpeed = .15
### END OF VARS ###

class MovePlatform(object):
    speed = 0
    def __init__(self,sur,img,x=0,y=0):
        self.image = pygame.image.load(img).convert_alpha()
        self.height = self.image.get_height()
        self.width = self.image.get_width()
        self.x = x
        self.y = y
        self.surface = sur
        self.draw()
    def draw(self):
        self.cutCoordinates()
        self.surface.blit(self.image,(self.x,self.y))
    def move(self,x,y):
        self.draw(self.x,self.y)
        self.x = x
        self.y = y
    def get_x(self):
        return self.x
    def get_y(self):
        return self.y
    def cutCoordinates(self):
        if self.x <0. :
            self.x = 0.
        elif self.x > float(self.surface.get_size()[0]-self.image.get_width()):
            self.x = float(self.surface.get_size()[0]-self.image.get_width())
        if self.y < 0.:
            self.y = 0.
        elif self.y > float(self.surface.get_size()[1]-self.image.get_height()):
            self.y = float(self.surface.get_size()[1]-self.image.get_height())



class PlayerUnit(MovePlatform):
    def __str__(self):
        return 'Player unit, current coords: x = '+str(self.get_x())+', y = '+str(self.get_y())
    def moveDir(self, direction='',distance=0):
        self.draw()
        if direction != '' and distance != 0:
            if direction == 'left':
                self.x -= distance
            if direction == 'right':
                self.x += distance
            if direction == 'up':
                self.y -= distance
            if direction == 'down':
                self.y += distance


class EnemyUnit(MovePlatform):

    def __init__(self,sur,img):

        MovePlatform.__init__(self,sur,img)
        self._position = Vector2(random.randrange(screenSize),random.randrange(screenSize))
        self._heading = Vector2()
        self.destination = Vector2(random.randrange(screenSize))- Vector2(self.surface.get_size())/2
        self._heading = Vector2.from_points(self.position, self.destination)

    def position():
        doc = "The position property."
        def fget(self):
            return self._position
        def fset(self, value):
            self._position = value
        def fdel(self):
            del self._position
        return locals()
    position = property(**position())

    def heading():
        doc = "The heading property."
        def fget(self):
            return self._heading
        def fset(self, value):
            self._heading = value
        def fdel(self):
            del self._heading
        return locals()
    heading = property(**heading())



    def __str__(self):
        return 'Enemy unit, current coords: x = '+str(self.get_x())+', y = '+str(self.get_y())
#    def moveDir(self, distance=0):
#        self.draw()
#        if distance != 0:
#            if 0 < self.dire < 90:
#                self.x += distance*cos(self.dire)
#                self.y += distance*cos(90-self.dire)
#            elif 90 < self.dire < 180:
#                self.x -= distance*cos(self.dire)
#                self.y += distance*cos(90-self.dire)
#            elif 180 < self.dire < 270:
#                self.x -= distance*cos(self.dire)
#                self.y -= distance*cos(90-self.dire)
#            elif 270 < self.dire < 360:
#                self.x += distance*cos(self.dire)
#                self.y -= distance*cos(90-self.dire)
#    def checkCoord(self):
##        print self.get_x()
#        if self.get_x() < 0 and self.dire < 180:
#            print "1: ",
#            print self.dire,
#            self.dire -= 90
#            print self.dire
#        elif self.get_x() < 0 and self.dire > 180:
#            print "2: ",
#            print self.dire,
#            self.dire += 90
#            print self.dire
#        elif self.get_x() > float(self.surface.get_size()[0]-self.image.get_width()) and self.dire < 180:
#            print "3: ",
#            print self.dire,
#            self.dire += 90
#            print self.dire
#        elif self.get_x() > float(self.surface.get_size()[0]-self.image.get_width()) and self.dire > 180:
#            print "4: ",
#            print self.dire,
#            self.dire -= 90
#            print self.dire
#
#        if self.get_y() < 0 and self.dire > 90:
#            print "5: ",
#            print self.dire,
#            self.dire += 90
#            print self.dire
#        elif self.get_y() < 0 and self.dire < 90:
#            print "6: ",
#            print self.dire,
#            self.dire = 360 - self.dire
#            print self.dire
#        elif self.get_y() > float(self.surface.get_size()[0]-self.image.get_width()) and 270 < self.dire < 360:
#            print "7: ",
#            print self.dire,
#            self.dire = 360 - self.dire
#            print self.dire
#        elif self.get_y() > float(self.surface.get_size()[0]-self.image.get_width()) and 180 < self.dire < 270:
#            print "8: ",
#            print self.dire,
#            self.dire -= 90
#            print self.dire

#        elif self.get_x() > 300:
#            self.dire -= 90
#        elif self.get_y() < 0:
#            self.dire += 90
#        elif self.get_y() > 300:
#            self.dire -= 90



def main():
    backImgFilename = 'sur.jpg'
    spriteImgFilename = 'obj.png'
    enemyImgFilename = 'enemy.png'

    pygame.init()

    screen = pygame.display.set_mode((screenSize, screenSize), 0, 32)
    print 'INFO', screen.get_size()
    background = pygame.image.load(backImgFilename).convert()
    clock = pygame.time.Clock()
    screen.blit(background,(0,0))

    test = {}
    number = 11
    for i in range(0,number):
        test[i] = PlayerUnit(screen,spriteImgFilename,i*25,i*25)
    print test

    enemies = []
    dire = []
    for i in range(numberEnemies):
#            xCoord = random.randrange(300)
#            yCoord = random.randrange(300)
            enemies += [EnemyUnit(screen,enemyImgFilename)]

    pygame.display.update()
    direction = ''

    while True:

        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
            elif event.type == KEYDOWN:
                if event.key == K_LEFT:
                    direction = 'left'
                if event.key == K_RIGHT:
                    direction = 'right'
                if event.key == K_UP:
                    direction = 'up'
                if event.key == K_DOWN:
                    direction = 'down'
       # print direction



        screen.blit(background,(0,0))

        timePassed = clock.tick()
        timePassedSeconds = timePassed / 1000.0
        distanceMoved = timePassedSeconds * speed



        for i in range(0,number):
            test[i].moveDir(direction,distanceMoved)

        for i in range(numberEnemies):
            enemies[i].heading.normalize()
            enemies[i].position = enemies[i].heading * distanceMoved

        pygame.display.update()


if __name__ == "__main__":
    main()
